// pages/applycrm/approval/approvaldetail.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require("../../../utils/share.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        piarr: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十'],
        leixing:1,   //2：换课  1：退费
        pricefalse:true,
        closefalse: false,    //拒绝理由弹窗
        data:'',
        phone:'',
        showfalse1:false,
        showfalse2:false,
        msg:'',
        yesfalse:true,
        noyesfalse:true,
        coursetype:1,
        shenfalse:false,
        shadminsrr:[],
    },

    changetap(){
        var that = this;
        var data = that.data.data;
        var leixing = that.data.leixing;
        if (Number(leixing)==1){
            wx.navigateTo({
                url: '/pages/staff/chooseCourse/applyreturn/applyreturn?stu_id=' + data.user_course_id+'',
            })
        }else{
            wx.navigateTo({
                url: '/pages/staff/chooseCourse/applychange/applychange?stu_id=' + data.user_course_id + '',
            })
        }
    },

    cetap(){
        var that = this;
        var data = that.data.data;
        wx.showLoading({
            title: '加载中~',
        })
        var leixing = that.data.leixing;
        var obj = {
            audit_id: data.audit_id,//	是	int	审核表ID
            type: leixing,//	是	int	1 - 退费，2-换课
            is_type:2,//	是	int	状态：1拒绝 2撤销
        }
        share.wxRequest('crmapi/user/changeRefundReject', obj, function (res) {
            if (Number(res.data.errcode) == 0) {
                wx.showToast({
                    title: '成功',
                    icon: 'none',
                    duration: 1500
                })
                that.details();
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 1500
                })
            }
            wx.hideLoading();
        })
    },

    yshr_admins2(e){
        var that = this;
        // console.log(e);
        var item = e.currentTarget.dataset.item;
        var index = e.currentTarget.dataset.index;
        var shrarr = item.shr_admins;
        // var admins2 = item.admins2;
        // var yshr_admins2 = item.yshr_admins2;
        // console.log();
        if(Number(item.status)>=2){
            var admins2 = item.yshr_admins2;
        }else{
            var admins2 = item.admins2;
        }
        var shadminsrr = [];
        if (Number(shrarr.length)>1){
            // console.log(shrarr)
            wx.showLoading({
                title: '加载中~',
            })
            for (var i = 0; i < shrarr.length;i++){
                var obj = {
                    name: shrarr[i].name,
                    status: 1,
                }
                if (Number(admins2.length)>0){
                    for (var j = 0; j < admins2.length; j++) {
                        if (Number(shrarr[i].id) == Number(admins2[j].id)){
                            var obj = {
                                name: shrarr[i].name,
                                status: 2,
                            }
                        }
                    }
                }
                shadminsrr.push(obj);
            }
            wx.hideLoading();
            // console.log(shadminsrr)
            that.setData({
                shadminsrr: shadminsrr,
                shenfalse:true,
            })
        }
    },
    shoclose(){
        var that = this;
        that.setData({
            shenfalse:false,
        })
    },

    sureclick1(){
        var that = this;
        that.setData({
            showfalse1: false,
        })
        wx.navigateBack(-1)
    },
    sureclick2() {
        var that = this;
        that.setData({
            showfalse2: false,
        })
    },

    // 拒绝理由点击确定
    yesbutclick1(e){
        var that = this;
        that.setData({
            closefalse:false
        })
        var noyesfalse = that.data.noyesfalse;
        if (noyesfalse){
            that.setData({
                noyesfalse:false
            })
            wx.showLoading({
                title: '提交中~',
            })
            var textnote = e.detail.value.textnote;
            var id = that.data.id;
            var leixing = that.data.leixing;
            var phone = that.data.phone;
            wx.request({
                url: CONFIG.API_HOST + 'crm/Roleauth/reject',
                data: {
                    id: id,  //
                    leixing: leixing,
                    phone: phone,
                    reject_reason: textnote
                },
                method: 'GET',
                success: function (res) {
                    wx.hideLoading();
                    if (res.data.status == 1001 || res.data.status == '1001') {
                        that.setData({
                            showfalse1: true,
                            msg: res.data.msg
                        })
                    } else {
                        that.setData({
                            showfalse2: true,
                            msg: res.data.msg,
                            noyesfalse: true
                        })
                    }
                }
            })
        }else{
            wx.showToast({
                title: '正在提交，请稍后~',
                icon: 'none',
                duration: 1500
            })
        }
    },
    

    // 点击拒绝申请
    nobutclick(){
        var that = this;
        that.setData({
            closefalse: true,
        })
    },
    
    // 拒绝理由点击取消
    closeclick(){
        var that = this;
        that.setData({
            closefalse:false,
        })
    },

    // 修改价格
    // priceclick(){
    //     var that = this;
    //     that.setData({
    //         pricefalse:false,
    //     })
    // },

    details() {
        var that = this;
        var id = that.data.id;
        var leixing = that.data.leixing;
        var obj = {
            audit_id:id,
            type: leixing
        }
        if (Number(app.globalData.identity) == 3) {
            var url = 'crmapi/Learn/changeRefundDetails'
        } else {
            var url = 'crmapi/user/changeRefundDetails'
        }
        share.wxRequest(url, obj, function (res) {
            wx.hideLoading();
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                that.setData({
                    data: data,
                })
            } else {
                that.setData({
                    errcode: res.data.status,
                    msg: res.data.msg,
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            leixing: options.leixing,
            id: options.id,
            phone:wx.getStorageSync('tel'),
            coursetype: options.coursetype
        })
        wx.showLoading({
            title: '加载中~',
        })
        that.details();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // wx.showLoading({
        //     title: '加载中~',
        // })
        // that.details();
    },

    // 点击同意申请
    yesbutclick(){
        var that = this;
        wx.showLoading({
            title: '提交中~',
        })
        that.setData({
            yesfalse:false
        })
        var id = that.data.id;
        var leixing = that.data.leixing;
        var phone = that.data.phone;
        wx.request({
            url: CONFIG.API_HOST + 'crm/Roleauth/confirm',
            data: {
                id: id,  //
                leixing: leixing,
                phone: phone
            },
            method: 'GET',
            success: function (res) {
                wx.hideLoading();
                if (res.data.status == 1001 || res.data.status == '1001') {
                    var data = res.data.data;
                    that.setData({
                        showfalse1: true,
                        msg:res.data.msg
                    })
                } else {
                    that.setData({
                        showfalse2: true,
                        msg: res.data.msg,
                        yesfalse: true
                    })
                }
            }
        })
    },

})